import axios from 'axios'
import { Loading } from 'element-ui'


export function request(config) {
    // 1.创建axios实例
    const instance = axios.create({
        baseURL: '/api',
        timeout:50000
    })

    let loading
    let loadingCount=0
    // 2.axios拦截器
    instance.interceptors.request.use(config => {
        config.headers.Authorization = window.sessionStorage.getItem('token')
        loadingCount++
        loading = Loading.service({
        target: 'body',
        background: 'transparent',
        text: '载入中',
      })
        return config
    })

    instance.interceptors.response.use(config => {
        loadingCount--
        if (loadingCount<=0) {
            loading.close()
        } else {
            console.log(111)
        }
        return config
    })

    // 3.发送真正的网络请求
    return instance(config)
}

